/**
 * Copyright 2014,Peak Tai 台俊峰(taijunfeng_it@sina.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cenluan.gallery;

import java.util.Date;

import com.cenluan.common.Const;
import com.cenluan.ext.jfinal.PkController;
import com.cenluan.gallery.member.GalleryMember;
import com.cenluan.kit.UUIDKit;
import com.cenluan.kit.XssKit;
import com.cenluan.login.ClearLoginInterceptor;
import com.cenluan.me.message.Message;
import com.cenluan.photo.Photo;
import com.cenluan.user.User;
import com.cenluan.user.log.UserLog;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;

/**
 * 相册
 * 
 * @author peak
 * 
 */
public class GalleryController extends PkController {
	/**
	 * 相册列表,show参数各个值含义:<br>
	 * 1.new，最新的，结果按创建时间倒序排列.<br>
	 * 2.pc,照片最多(photo count)，结果按照片数量倒序排列.<br>
	 * 3.mine,显示我的相册
	 */
	@ClearLoginInterceptor
	public void index() {
		User user = getSessionAttr(Const.SESSION_USER);
		String show = getPara("show");
		String order = "new";
		String createrId = null;
		if ("pc".equals(show)) {
			order = "pc";
		} else if (user != null && "mine".equals(show)) {
			createrId = user.getStr("id");
		}

		Page<Gallery> page = Gallery.dao.page(getPn(), 24, getPara("title"),
				createrId, order);
		setPage(page);
		keepPara();
	}

	/**
	 * 展示
	 */
	@ClearLoginInterceptor
	public void show() {
		User user = getSessionAttr(Const.SESSION_USER);
		Gallery gallery = Gallery.dao.findById(getPara());
		if (gallery == null) {
			renderError(404);
			return;
		}
		if (!Gallery.STATE_ACTIVE.equals(gallery.getStr("state"))) {
			renderError(404);
			return;
		}
		setAttr("gallery", gallery);
		setAttr("memberList", gallery.getMemberList());
		setAttr("disabledMemberList", gallery.getDisabledMemberList());
		setAttr("creater",
				user != null
						&& user.getStr("id")
								.equals(gallery.getStr("createrId")));
		GalleryMember member = null;
		if (user != null) {
			member = GalleryMember.dao.findBygalleryIdAndUserId(
					gallery.getStr("id"), user.getStr("id"));
		}
		setAttr("joined", member != null);
		setAttr("enabled", member != null && member.getBoolean("active"));
	}

	/**
	 * 添加
	 */
	public void add() {
		createToken();
	}

	/**
	 * 保存
	 */
	@Before(GalleryValidator.class)
	public void save() {
		User user = getSessionAttr(Const.SESSION_USER);
		Gallery gallery = getModel(Gallery.class);
		gallery.set("id", UUIDKit.generateUUID());
		gallery.set("state", Gallery.STATE_ACTIVE);
		gallery.set("createTime", new Date());
		gallery.set("createrId", user.get("id"));
		gallery.save();
		redirect("/gallery/show/" + gallery.getStr("id"));
		// 用户动态
		UserLog.save(user.getStr("id"), UserLog.TYPE_GALLERY,
				gallery.getStr("id"));

	}

	/**
	 * 编辑
	 */
	public void edit() {
		User user = getSessionAttr(Const.SESSION_USER);
		Gallery gallery = Gallery.dao.findById(getPara());
		if (!Gallery.STATE_ACTIVE.equals(gallery.getStr("state"))) {
			renderError(403);
			return;
		}
		if (!user.getStr("id").equals(gallery.getStr("createrId"))) {
			renderError(403);
			return;
		}
		setAttr("gallery", gallery);
	}

	/**
	 * 更新
	 */
	@Before(GalleryValidator.class)
	public void update() {
		User user = getSessionAttr(Const.SESSION_USER);
		Gallery gallery = getModel(Gallery.class);
		Gallery old = Gallery.dao.findById(gallery.getStr("id"));
		if (!Gallery.STATE_ACTIVE.equals(old.getStr("state"))) {
			renderError(403);
			return;
		}
		if (!user.getStr("id").equals(old.getStr("createrId"))) {
			renderError(403);
		}
		gallery.keep("id", "title", "intro", "pwd");
		gallery.update();
		setAttr("ok", true);
		renderJson();

	}

	/**
	 * 删除
	 */
	public void delete() {
		User user = getSessionAttr(Const.SESSION_USER);
		Gallery gallery = Gallery.dao.findById(getPara());
		if (!user.isAdmin()
				&& !user.getStr("id").equals(gallery.getStr("createrId"))) {
			renderError(403);
			return;
		}

		// 非相册创建者自己删除的发通知
		if (!user.getStr("id").equals(gallery.getStr("createrId"))) {
			Message.save(user.getStr("id"), gallery.getStr("createrId"),
					"将您的相册\"" + XssKit.escapeStr(gallery.getStr("title"))
							+ "\"删除");
		}

		gallery.set("state", Gallery.STATE_DELETED);
		gallery.keep("id", "state").update();
		setAttr("ok", true);
		renderJson();

	}

	/**
	 * 设置封面
	 */
	public void setCover() {
		User user = getSessionAttr(Const.SESSION_USER);
		Gallery gallery = Gallery.dao.findById(getPara(0));
		if (!user.getStr("id").equals(gallery.getStr("createrId"))) {
			renderError(403);
			return;
		}
		Photo photo = Photo.dao.findById(getPara(1));
		if (!photo.getStr("galleryId").equals(gallery.getStr("id"))) {
			renderError(403);
			return;
		}
		gallery.set("coverId", getPara(1)).keep("id", "coverId").update();
		setAttr("ok", true);
		renderJson();

	}
}
